package controllers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import model.ComplaintReportModel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import view.ViewYearlyGUI;
import client.CliMessage;

// TODO: Auto-generated Javadoc
/**
 * The Class ViewYearlyController that extends AbstractController implements IValidateInput.
 */
public class ViewYearlyController extends AbstractController implements IValidateInput
{	
	
	/** The Yearly view. */
	private ViewYearlyGUI YearlyView;
	
	/** The ComplaintReportModel report model. */
	private ComplaintReportModel CompReportModel;
	private static int flag = 0;
	
	/**
	 * This is ViewYearlyController controller constructor.
	 *
	 * @param YearlyView the yearly view
	 * @param CompReportModel the comp report model
	 */
	
	public ViewYearlyController(ViewYearlyGUI YearlyView, ComplaintReportModel CompReportModel) 
	{
		super(YearlyView,CompReportModel);
		this.YearlyView = YearlyView;
		this.CompReportModel = CompReportModel;
		
		this.YearlyView.addButtonListener1(new Button1Listener());
		this.YearlyView.addButtonListener2(new Button2Listener());
		this.YearlyView.addButtonListener3(new Button3Listener());

	}
	
	@Override
	public boolean validateInput() 
	{
		return true;		
	}
	
	class Button1Listener extends AbstractListener
	{
		public Button1Listener ()
		{	
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{		
			String searchQuery = "search"	+ "SELECT * "
                                            + "FROM daily_statistc";
			
			ViewYearlyController.flag = 1;			                 
            sendToServer(searchQuery);				                       											
		}
	}
	
	class Button2Listener extends AbstractListener
	{
		public Button2Listener ()
		{	
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{		
			String searchQuery = "search"	+ "SELECT * "
                                            + "FROM package_report";
			
			ViewYearlyController.flag = 2;						                 
            sendToServer(searchQuery);				                       											
		}
	}
	
	class Button3Listener extends AbstractListener
	{
		public Button3Listener ()
		{	
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{		
			String searchQuery = "search"	+ "SELECT * "
                                            + "FROM package_report";
			
			ViewYearlyController.flag = 3;						                 
            sendToServer(searchQuery);				                       											
		}
	}
	

	@Override
	public void handleDBRequestSuccess()
	{	
	}

	@Override
	public void handleDBSearchResult(CliMessage dbResult) 
	{	
		ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
		Map<String, String> weeklyTupple  = srs.get(srs.size() -1);
		
		int [] yearlyArrayMem = new int [365];
		for(int i = 0 ; i < 365  ; i++)
			yearlyArrayMem[i] = 0;
		
		int [] yearlyArrayPack = new int [12];
		for(int i = 0 ; i < 12  ; i++)
			yearlyArrayPack[i] = 0;
		
		double [] yearlyArrayUsage = new double [12];
		for(int i = 0 ; i < 12  ; i++)
			yearlyArrayUsage[i] = 0;
		
		Calendar cal = Calendar.getInstance();	
		int currMonth = cal.get(Calendar.MONTH);
		currMonth++;
		int currYear = cal.get(Calendar.YEAR);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		
		if ( weeklyTupple.get("MEDIANREG") != null )
		{
		String medReg = weeklyTupple.get("MEDIANREG");
		String standReg = weeklyTupple.get("STANDARDREG");;
		String medSub = weeklyTupple.get("MEDIANSUB");
		String standSub = weeklyTupple.get("STANDARDSUB");
		String medUsage = weeklyTupple.get("MEDIANUSAGE");
		String standUsage = weeklyTupple.get("STANDARDUSAGE");
		String DATE = weeklyTupple.get("DATE").substring(0,16);
		
		
		///set fields GUi
		YearlyView.SetGenLabel(medReg,  YearlyView.getlabel1_2());
		YearlyView.SetGenLabel(standReg,  YearlyView.getlabel2_2());
		YearlyView.SetGenLabel(medSub,  YearlyView.getlabel4_2());
		YearlyView.SetGenLabel(standSub,  YearlyView.getlabel5_2());
		YearlyView.SetGenLabel(medUsage,  YearlyView.getlabel7_2());
		YearlyView.SetGenLabel(standUsage,  YearlyView.getlabel8_2());
		YearlyView.SetGenLabel(DATE,  YearlyView.date_Label());
		}
		else
		{
			if (flag == 1)
			{
			   	for(int i = 0 ; i < srs.size()  ; i++)
			   	{
			   	      Map<String, String> Tupple  = srs.get(i);	
			   			
			 		  int DBmonth = (Integer.parseInt((Tupple.get("DATE")).substring(5, 7)));
					  int DByear = (Integer.parseInt((Tupple.get("DATE")).substring(0, 4)));
					  int DBday = (Integer.parseInt((Tupple.get("DATE")).substring(8, 10)));
					   
			 		  if (currYear == DByear)
			 		  {
			 			 yearlyArrayMem [ DBday + (DBmonth * 30) -1] = Integer.parseInt((Tupple.get("MEMBERNUM")));
			 		  }		 		  
			 		  
				}
			   	
			   	for (int i = 0; i<365; i++)
			   		System.out.println (yearlyArrayMem[i]+ " **** ");
		 		  
	 			  DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	 			  for (int i = 0; i < 365; i++)
		 			  dataset.setValue(yearlyArrayMem[i] ,"members" , "'"+ (i + 1) +"'" );
	 			  JFreeChart chart = ChartFactory.createBarChart("Members", "Day", "Members", dataset, PlotOrientation.VERTICAL, false, true, false);
	 			  CategoryPlot p = chart.getCategoryPlot();
	 			  p.setRangeGridlinePaint(Color.black);
	 			  ChartFrame frame = new ChartFrame ("Frequency distribution of members", chart);
	 			  frame.setVisible(true);
	 			  frame.setSize (700,700);
			}
			
			if (flag == 2)
			{
				
			   	for(int i = 0 ; i < srs.size()  ; i++)
			   	{
			   	      Map<String, String> Tupple  = srs.get(i);	
			   			
			 		  int DBmonth = (Integer.parseInt((Tupple.get("DATE")).substring(5, 7)));
					  int DByear = (Integer.parseInt((Tupple.get("DATE")).substring(0, 4)));
					   
			 		  if (currYear == DByear)
			 		  {
			 			 yearlyArrayPack [ DBmonth-1 ] = yearlyArrayPack [ DBmonth-1 ] + Integer.parseInt((Tupple.get("NEWPACK")));
			 		  }
			   	}
			 		  
			   	for (int i = 0; i<12; i++)
				System.out.println (yearlyArrayPack[i]+ " **** ");
						  
			 	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			 	for (int i = 0; i < 12; i++)
			 		dataset.setValue(yearlyArrayPack[i] ,"New Package" , "'"+ (i + 1) +"'" );
			 	JFreeChart chart = ChartFactory.createBarChart("New Package", "Month", "New Package", dataset, PlotOrientation.VERTICAL, false, true, false);
			 	CategoryPlot p = chart.getCategoryPlot();
			 	p.setRangeGridlinePaint(Color.black);
			 	ChartFrame frame = new ChartFrame ("Frequency distribution of members", chart);
			 	frame.setVisible(true);
			 	frame.setSize (700,700);
			}
			
			if (flag == 3)
			{
				
			   	for(int i = 0 ; i < srs.size()  ; i++)
			   	{
			   	      Map<String, String> Tupple  = srs.get(i);	
			   			
			 		  int DBmonth = (Integer.parseInt((Tupple.get("DATE")).substring(5, 7)));
					  int DByear = (Integer.parseInt((Tupple.get("DATE")).substring(0, 4)));
					   
			 		  if (currYear == DByear)
			 		  {
			 			 yearlyArrayUsage [ DBmonth -1] = yearlyArrayUsage [ DBmonth -1] + Integer.parseInt((Tupple.get("PACKUSAGE")));
			 		  }
			   	}
			   	
			   	for (int i = 0; i < 12 ; i++)
			   		yearlyArrayUsage[i] = Math.floor(yearlyArrayUsage[i] * 100) / 100;
			 		  
			   	for (int i = 0; i<12; i++)
				System.out.println (yearlyArrayUsage[i]+ " **** ");
						  
			 	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			 	for (int i = 0; i < 12; i++)
			 		dataset.setValue(yearlyArrayUsage[i] ,"New Package" , "'"+ (i + 1)+"'" );
			 	JFreeChart chart = ChartFactory.createBarChart("Yearly Usage", "Month", "Usage per %", dataset, PlotOrientation.VERTICAL, false, true, false);
			 	CategoryPlot p = chart.getCategoryPlot();
			 	p.setRangeGridlinePaint(Color.black);
			 	ChartFrame frame = new ChartFrame ("Frequency distribution of Pack Usage", chart);
			 	frame.setVisible(true);
			 	frame.setSize (700,700);
				
			}
			
		}


		YearlyView.setVisible(true);						
	}

	@Override
	public void handleNullSearchResult() 
	{
		YearlyView.displayWrnMessage("DATA DOES NOT EXIST!");	
	}

}
